﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace QuanLyThuNgan
{
    public partial class Frm_XacNhanHetCa : DevExpress.XtraEditors.XtraForm
    {
        public Frm_XacNhanHetCa()
        {
            InitializeComponent();
        }

        DMHetCaCTL hetCaCtl = new DMHetCaCTL();
        DMHetCaDTO hetCaDto = new DMHetCaDTO();
        CaThuNganCTL caThuNganCtl = new CaThuNganCTL();
        CaThuNganDTO caThuNganDto = new CaThuNganDTO();
        int iIDNguoiDung = frmLogin.nd.IDNguoiDung;
        int iIDCaTN;
        public static dsInThuTien ds;
        private int iloai = 0;

        private void Frm_XacNhanHetCa_Load(object sender, EventArgs e)
        {


            DataTable dtHetCa = hetCaCtl.m_SelectHetCa();
            ClassHelp.m_gridLookUpEdit(dtHetCa, gdTinhTrangHC, "TenHetCa", "IDHetCa");
            // lay idnguoidung--------------------------
            int iIDNguoiDung = frmLogin.nd.IDNguoiDung;
            //kiem tra nguoi dung het ca-----------------------------------
            DataTable dtkt = caThuNganCtl.m_SelectKiemTraNguoiDungTTXV(iIDNguoiDung);
            iloai = Uit.it_Parse.ToInteger(dtkt.Rows[0]["loai"].ToString());
            if (iloai >= 1)
            {
                btnXacNhan.Enabled = true;
                btnInCa.Enabled = true;
            }
            else
            {
                btnXacNhan.Enabled = false;
                btnInCa.Enabled = false;
            
            }
            //load thong tin het ca----------------------------------------------
            DataTable dthc = caThuNganCtl.m_SelectAll_XacNhanHetCa(iIDNguoiDung);
            foreach (DataRow dr in dthc.Rows)
            {
                caThuNganDto.IIDCaThuNgan= Uit.it_Parse.ToInteger(dr["IDCaThuNgan"].ToString());
                txtTenCa.Text = dr["TenCaLamViec"].ToString();
                txtNgayDK.Text = Uit.it_Parse.ToDateTime(dr["NgayDangKy"]).ToString("dd/MM/yyyy");
                txtGhiChu.Text = dr["GhiChu"].ToString();
                iIDCaTN = Uit.it_Parse.ToInteger(dr["IDCaThuNgan"].ToString());
            }
            if (txtTenCa.Text != "")
            {
                txtNgayHetCa.Text = Uit.it_Date.CurrentDate();
            }
            else
            {
                txtNgayHetCa.Text = "";
            }
            
            m_ReadOnly();
        }

        //set readonly-----------------------------
        void m_ReadOnly()
        {
            txtNgayDK.Properties.ReadOnly = true;
            txtTenCa.Properties.ReadOnly = true;
            txtNgayHetCa.Properties.ReadOnly = true;
            txtGhiChu.Properties.ReadOnly = true;
        }

        //set readonlyall
        void m_ReadOnlyAll()
        {
            gdTinhTrangHC.Properties.ReadOnly = true;
            txtNgayDK.Properties.ReadOnly = true;
            txtTenCa.Properties.ReadOnly = true;
            txtNgayHetCa.Properties.ReadOnly = true;
            txtGhiChu.Properties.ReadOnly = true;
        }

        //xac nhan het ca lam viec-------------------------------
        private void btnXacNhan_Click(object sender, EventArgs e)
        {
            if (gdTinhTrangHC.EditValue != null)
            {
                caThuNganDto.IIDHetCa = Uit.it_Parse.ToInteger(gdTinhTrangHC.EditValue.ToString());
                caThuNganDto.DaNgayHetCa = Uit.it_Date.CurrentDate("yyyy-MM-dd");
                caThuNganDto.SsGioHetCa = Uit.it_Date.CurrentTime();
                caThuNganCtl.m_UpdateXacNhanHC(caThuNganDto);

            }
            XtraMessageBox.Show("Xác nhận thành công");
            m_ReadOnlyAll();
            btnXacNhan.Enabled = false;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnInCa_Click(object sender, EventArgs e)
        {
            ds = new dsInThuTien();
            //DataRow dr = ds.InHetCa.NewRow();
            DataTable dt = caThuNganCtl.m_SelectChiTietNopTien(iIDCaTN);
            
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["Loai"].ToString() == "1")
                {
                    ds.InHetCa.Rows.Add(dt.Rows[i]["STT"].ToString(), dt.Rows[i]["TenMucPhi"].ToString(), dt.Rows[i]["DaXacNhan"].ToString(), dt.Rows[i]["ChuaXacNhan"].ToString());
                }
            }
            //load form In het ca-------------------------------------------
            Form f = FormHelpers.CheckChildFormShow("Frm_InHetCa", this);
            if (f == null)
            {
                Frm_InHetCa frm = new Frm_InHetCa();
                frm.MdiParent = this.ParentForm;
                frm.WindowState = FormWindowState.Maximized;
                frm.Show();
            }
            else
            {
                f.Activate();
                f.Focus();
            }
            //this.Close();
            
        }
    }
}